﻿using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Service
{
    public class SetAlarmService:BaseService,ISetAlarmService
    {
        #region
        [Autowired]
        private  ISetAlarmRepository setAlarmRepository { get; set; }
        [Autowired]
        private  IMapper mapper { get; set; }
        public SetAlarmService()
        {
        }

        #endregion
        /// <summary>
        /// 获取预警设置
        /// </summary>
        /// <returns></returns>
        public async Task<BaseMessage> GetSetAlarm()
        {
            IQueryable<SetAlarmView> setAlarms = setAlarmRepository.GetSetAlarms();
            return Ok200("查询成功!",await setAlarms.FirstOrDefaultAsync());
        }
        /// <summary>
        /// 更新设置
        /// </summary>
        /// <param name="model"></param>
        /// <param name="ID"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>

        public async Task<BaseMessage> UpdateAlarm(UpdateSetAlarm model,int ID,int UserID)
        {
            SetAlarmEntity entity =await setAlarmRepository.FirstOrDefaultAsync(t=>t.ID==ID);
            mapper.Map(model,entity);
            var entityEntry=await setAlarmRepository.Update(entity,UserID);
            if (await setAlarmRepository.SaveChangesAsync())
            {
                return Ok200("更新成功!");
            }
            return BadRequest400("更新失败!");
        }
    }
}
